﻿<script>


    $(function () {
        var _vm = new ViewModel();
        _vm.Init();
    });

    function ViewModel() {
        var me = this;

        //注意顺序
        me.DataTable = new PagedDataTable(me,"#table");
        me.Dialog = new Dialog(me,"#frmEdit");
        me.app = new App(me, "#app");
        me.app.DeleteUrl = "@this.Href("~/SystemManage/Role/Delete")";
        Extend.call(me);
    }
    function Extend() {
        var me = this;
         me.Init = function () {
            me.LoadModels();
        }
        me.LoadModels = function (page) {
            me.DataTable.SelectedModel = null;
            var data = me.app.SearchModel;
            data.page = page || 1;
            $hits.get("@this.Href("~/SystemManage/Role/GetModels")", data, function (result) {
                me.DataTable.SetPagedData(result.Data);
            }
         );
         }
        me.OnOpen = function () {
            initControl(me.Dialog.Model.Id);
        }
         me.OnSave = function () {
            var model = me.Dialog.Model;
            if (!$('#form1').formValid()) {
                return false;
            }

            var permissionIdArr = $("#permissionTree").getCheckedNodes();
            var permissionIds = "";
            var c = "";
            for (var i = 0; i < permissionIdArr.length; i++) {
                permissionIds += c + permissionIdArr[i];
                c = ",";
            }
            model.permissionIds = permissionIds;

            if (me.Dialog.EditModel) {
                $hits.post("@this.Href("~/SystemManage/Role/Update")", model, function (result) {
                    $hits.msg(result.Msg);
                    me.Dialog.Close();
                    me.LoadModels();
                }
               );
            }
            else {
                $hits.post("@this.Href("~/SystemManage/Role/Add")", model, function (result) {
                    $hits.msg(result.Msg);
                    me.Dialog.Close();
                    me.LoadModels();
                }
             );
            }
        }

    }
    function initControl(Id) {
        var w = $('#wizard').wizard();
        w.on('change', function (e, data) {
            var $finish = $("#btn_finish");
            var $next = $("#btn_next");
            if (data.direction == "next") {
                switch (data.step) {
                    case 1:
                        if (!$('#form1').formValid()) {
                            return false;
                        }
                        $finish.show();
                        $next.hide();
                        break;
                    default:
                        break;
                }
            } else {
                $finish.hide();
                $next.show();
            }
        });
        $("#permissionTree").html(null);
        $("#permissionTree").treeview({
            height: 444,
            showcheck: true,
            url: "/SystemManage/RoleAuthorize/GetPermissionTree",
            param: { roleId: Id },
            cbiconpath: "@this.Href("/Content/wdtree/images/icons/")"
        });

    }
</script>

